﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Media;

namespace DoAnHuongRung
{
    public partial class frmNhaCungCap : Form
    {
        public frmNhaCungCap()
        {
            InitializeComponent();
        }
        SoundPlayer s;

        private void frmNhaCungCap_Load(object sender, EventArgs e)
        {
            LoadDtgNhaCC();
            //dtgNhaCC.Rows[1].Selected = true;
            LoadDataTxtTimkiem();
        }

        private void LoadComboboxNguyenVatLieu(int MaNCC)
        {
            cmbNVL.ValueMember = "MaNVL";
            cmbNVL.DisplayMember = "TenNVL";
            cmbNVL.DataSource = NguyenVatLieuBUS.Select_NVL_not_exits_in_NCC(MaNCC);
        }

        private void LoadDtgNhaCC()
        {
            dtgNhaCC.AutoGenerateColumns = false;
            dtgNhaCC.DataSource = NhaCungCapBUS.SelectAll();
        }

        private void LoadDataTxtTimkiem()
        {
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            List<NhaCungCapDTO> list = NhaCungCapBUS.SelectAll();
            for (int i = 0; i < list.Count(); i++)
                auto.Add(list[i].TenNCC);
            txtTimKiemNCC.AutoCompleteCustomSource = auto;
            
        }

        private void LoadDtgNVL(int MaNCC)
        {
            List<NguyenVatLieuDTO> listnvl = NguyenVatLieuBUS.Select_by_MaNCC(MaNCC);
            dtgNVL.AutoGenerateColumns = false;
            dtgNVL.DataSource = listnvl;
            for (int i = 0; i < listnvl.Count(); i++)
            {
                dtgNVL.Rows[i].Cells[2].Value = "Xóa";
            }
        }

        private void dtgNhaCC_SelectionChanged(object sender, EventArgs e)
        {
            string MaNCC = dtgNhaCC.CurrentRow.Cells[0].Value.ToString();
            LoadComboboxNguyenVatLieu(int.Parse(MaNCC));
            LoadDtgNVL(int.Parse(MaNCC));
            txtDiaChi.Text = dtgNhaCC.CurrentRow.Cells[2].Value.ToString();
            txtSDT.Text = dtgNhaCC.CurrentRow.Cells[3].Value.ToString();
            txtTenNCC.Text = dtgNhaCC.CurrentRow.Cells[1].Value.ToString();
        }

        private void dtgNVL_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string str = ((System.Windows.Forms.DataGridView)(sender)).Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            if (str.Equals("Xóa"))
            {
                
                bool b = NguyenVatLieuBUS.Delete_NVL_from_NCC(int.Parse(dtgNVL.CurrentRow.Cells[3].Value.ToString()));
                if (b == true)
                {
                    s = new SoundPlayer(Properties.Resources.Windows_Notify);
                    s.Play();
                    LoadDtgNVL(int.Parse(dtgNhaCC.CurrentRow.Cells[0].Value.ToString()));
                    LoadComboboxNguyenVatLieu(int.Parse(dtgNhaCC.CurrentRow.Cells[0].Value.ToString()));
                    //MessageBox.Show("Đã xóa !!!");
                }
            }
        }

        private void btnThemNVL_Click(object sender, EventArgs e)
        {
            int NVL = int.Parse(cmbNVL.SelectedValue.ToString());
            int NCC = int.Parse(dtgNhaCC.CurrentRow.Cells[0].Value.ToString());
            string DonGia = txtDonGia.Text;
            bool b = NguyenVatLieuBUS.Insert_NVL_for_NCC(NVL, NCC, DonGia);
            if (b == true)
            {
                LoadComboboxNguyenVatLieu(NCC);
                LoadDtgNVL(NCC);
                s = new SoundPlayer(Properties.Resources.Windows_Notify);
                s.Play();
                //MessageBox.Show("Đã thêm !!!");
            }
        }

        private void txtTimKiemNCC_Click(object sender, EventArgs e)
        {
            if (txtTimKiemNCC.Text.Equals("Tìm kiếm"))
            {
                txtTimKiemNCC.Text = "";
                txtTimKiemNCC.ForeColor = Color.Black;
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keydata)
        {
            if (keydata == Keys.Enter)
            {
                if (txtTimKiemNCC.Focused == true && !txtTimKiemNCC.Text.Equals(""))
                {
                    dtgNhaCC.DataSource = NhaCungCapBUS.Search_by_TenNCC(txtTimKiemNCC.Text);
                }
            }
            return false;
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            LoadDtgNhaCC();
            txtTimKiemNCC.Text = "Tìm kiếm";
            txtTimKiemNCC.ForeColor = Color.Gray;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                NhaCungCapDTO ncc = new NhaCungCapDTO();
                ncc.TenNCC = txtTenNCC.Text;
                ncc.DiaChi = txtDiaChi.Text;
                ncc.SoDienThoai = txtSDT.Text;
                bool b = NhaCungCapBUS.Insert(ncc);
                if (b == true)
                {
                    LoadDtgNhaCC();
                    s = new SoundPlayer(Properties.Resources.Windows_Notify);
                    s.Play();
                    MessageBox.Show("Đã thêm nhà cung cấp !!!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                s = new SoundPlayer(Properties.Resources.Windows_Notify_System_Generic);
                s.Play();
                DialogResult dr = MessageBox.Show("Chắc chắn xóa !!!", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dr == DialogResult.Yes)
                {
                    bool b = NhaCungCapBUS.Delete(int.Parse(dtgNhaCC.CurrentRow.Cells[0].Value.ToString()));
                    if (b == true)
                    {
                        LoadDtgNhaCC();
                        s = new SoundPlayer(Properties.Resources.Windows_Notify);
                        s.Play();
                        MessageBox.Show("Xóa thành công !!!");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                NhaCungCapDTO ncc = new NhaCungCapDTO();
                ncc.MaNCC = int.Parse(dtgNhaCC.CurrentRow.Cells[0].Value.ToString());
                ncc.TenNCC = txtTenNCC.Text;
                ncc.DiaChi = txtDiaChi.Text;
                ncc.SoDienThoai = txtSDT.Text;
                bool b = NhaCungCapBUS.Update(ncc);
                if (b == true)
                {
                    LoadDtgNhaCC();
                    //dtgNhaCC.Rows[vitriRow].Selected = true;
                    s = new SoundPlayer(Properties.Resources.Windows_Notify);
                    s.Play();
                    MessageBox.Show("Cập nhật thành công !!!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtTenNCC_Click(object sender, EventArgs e)
        {
            txtTenNCC.Text = "";
        }

        private void txtDiaChi_Click(object sender, EventArgs e)
        {
            txtDiaChi.Text = "";
        }

        private void txtSDT_Click(object sender, EventArgs e)
        {
            txtSDT.Text = "";
        }

        private void txtDonGia_Click(object sender, EventArgs e)
        {
            txtDonGia.Text = "";
        }
    }
}
